package net.adetech.cjg.innerservice.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import net.adetech.cjg.innerservice.entity.FrmCode;
import net.adetech.cjg.innerservice.mapper.FrmCodeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CommonController {

    @Autowired
    FrmCodeDao dao;

    @RequestMapping("/cjg/xzqh")
    public String getXzqh()
    {
        String result = "var xzqhOptions = ";

        List<FrmCode> frmCodeList = dao.getXzqhFrmCodeList();
        JSONArray jsonArray = new JSONArray();
        for (FrmCode code:frmCodeList
             ) {
            JSONObject codeJson = new JSONObject();
            if(code.getDmz().substring(4).equals("00")&&!code.getDmz().substring(2).equals("0000"))
            {
                codeJson.put("filter",code.getDmz().substring(0,2)+"0000");
            }
            if(!code.getDmz().substring(4).equals("00"))
            {
                codeJson.put("filter",code.getDmz().substring(0,4)+"00");
            }

            codeJson.put("key",code.getDmz());
            codeJson.put("value",code.getDmsm1());
            jsonArray.add(codeJson);

        }

        return result + jsonArray.toJSONString().replaceAll("\\{","\n\\{")+";";
    }

    @RequestMapping("/cjg/options")
    public String getOptions(String xtlb,String dmlb,String optionsName )
    {
        String result = "var "+optionsName+" = ";

        List<FrmCode> frmCodeList = dao.getFrmCodeList(dmlb,xtlb);
        JSONArray jsonArray = new JSONArray();
        for (FrmCode code:frmCodeList
                ) {
            JSONObject codeJson = new JSONObject();
            codeJson.put("key",code.getDmz());
            codeJson.put("value",code.getDmsm1());
            jsonArray.add(codeJson);
        }

        return result + jsonArray.toJSONString()+";";
    }



}
